package tq.com.tqstudent.ui.activity.mine;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.aries.ui.widget.alert.UIAlertDialog;
import com.lib.common.base.BaseActivity;
import com.lib.common.utils.AppManager;
import com.lib.common.utils.AppUtil;
import com.lib.common.utils.FileUtil;
import com.lib.common.utils.LogUtil;
import com.lib.common.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.UserInfo;
import tq.com.tqstudent.bean.event.LoginOutEvent;
import tq.com.tqstudent.ui.activity.HomeActivity;
import tq.com.tqstudent.utils.VarietyUtil;

import static android.content.DialogInterface.BUTTON_NEGATIVE;
import static android.content.DialogInterface.BUTTON_POSITIVE;

/**
 * @Author rames
 * @date 2019/3/5 17:22
 */
public class SettingActivity extends BaseActivity {
    @BindView(R.id.cache_tv)
    TextView cacheTV;
    @BindView(R.id.push_tv)
    TextView pushTV;
    public static final String PUSH_SWITCH = "PUSH_QT_SWITCH";

    public static void show(Activity activity) {
        Intent intent = new Intent(activity, SettingActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    public int initLayout() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("设置");
        getFileSize();
        pushTV.setText(SPUtils.getInstance(this).getBoolean(PUSH_SWITCH) ? "是" : "否");
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.logout_tv, R.id.cache_layout, R.id.score_layout, R.id.rule_layout, R.id.arrow4, R.id.push_tv})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.logout_tv:
                logout();
                break;

            case R.id.cache_layout:
                if(!cacheTV.getText().toString().equals("0 Byte")) {
                    showLoading();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            FileUtil.deleteFolderFile(VarietyUtil.getCrashLogPath(), true);
                            showToast(FileUtil.deleteFolderFile(Constants.DOWNLOAD_PDF_URL, false) ? "删除成功" : "删除失败");

                            getFileSize();
                            dismissLoading();
                        }
                    }, 1000);
                }else{
                    showToast("没有缓存文件");
                }
                break;

            case R.id.arrow4:
                findViewById(R.id.push_layout).setVisibility(View.VISIBLE);
                pushTV.setText(SPUtils.getInstance(this).getBoolean(PUSH_SWITCH) ? "是" : "否");
                break;

            case R.id.push_tv:
                boolean flag = SPUtils.getInstance(this).getBoolean(PUSH_SWITCH);
                SPUtils.getInstance(this).putBoolean(PUSH_SWITCH, !flag);
                pushTV.setText(SPUtils.getInstance(this).getBoolean(PUSH_SWITCH) ? "是" : "否");
                break;

            case R.id.score_layout:
                showLoading();
                break;

            case R.id.rule_layout:
                ProtocolActivity.show(this);
                break;
        }
    }

    private void getFileSize(){
        long pdfSize = FileUtil.getFolderSize(new File(Constants.DOWNLOAD_PDF_URL));
        long crashSize = FileUtil.getFolderSize(new File(VarietyUtil.getCrashLogPath()));
        LogUtil.e("pdf: "+AppUtil.getStringSize(pdfSize)+" | crash: "+AppUtil.getStringSize(crashSize));
        cacheTV.setText(AppUtil.getStringSize(pdfSize+crashSize));
    }

    private void logout(){
        new UIAlertDialog.DividerIOSBuilder(this)
                .setTitle("")
                .setMessage("确定退出?")
                .setMessageTextColorResource(R.color.black)
                .setNegativeButton("取消", onPhoneAlertClick)
                .setNegativeButtonTextColorResource(R.color.black)
                .setPositiveButton("确定", onPhoneAlertClick)
                .setPositiveButtonTextColorResource(R.color.black)
                .setPositiveButtonFakeBoldEnable(true)
                .setMinHeight(AppUtil.dp2px(this,100))
                .create()
                .setDimAmount(0.5f)
                .show();
    }

    private DialogInterface.OnClickListener onPhoneAlertClick = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case BUTTON_NEGATIVE:// 取消
                    break;
                case BUTTON_POSITIVE:// 确定
                    UserInfo.loginOut();
                    App.stopImWhenLogout();
                    EventBus.getDefault().post(new LoginOutEvent());
                    AppManager.getAppManager().popToActivity(HomeActivity.class);
                    break;
            }
        }
    };
}
